package pro.shuangxi.generator;

import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.Template;
import pro.shuangxi.pojo.RenderData;
import pro.shuangxi.source.MapSource;
import pro.shuangxi.utils.RenderDataUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author mengshx
 * @description <p>模板渲染器</p>
 * @date 2022-07-14 18:33
 **/
public class Generator {
    Pattern pattern = Pattern.compile("\\$\\{([^\\}]+)\\}");
    Map<String,Object> data;
    Configuration configuration = new Configuration(Configuration.getVersion());
    File output=null;

    public Generator(MapSource mapSource) {
        this(mapSource, "render");
    }
    public Generator(MapSource mapSource,String outputPath) {
        this(mapSource.getMap(), outputPath);
    }
    public Generator(Map<String,Object> map) {
        this(map, "render");
    }
    public Generator(Map<String,Object> map, String outputPath) {
        this.data = map;
        this.setOutputPath(outputPath);
    }
    public void setOutputPath(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        this.output=file;
    }
    public void render(){
        this.render("template");

    }
    public void render(String path){
        File file = new File(path);
        this.render(file);

    }
    public void render(File file) {
        if (file.exists()) {
            List<RenderData> fileList = new ArrayList<>();
            File directory=null;

            if (file.isDirectory()) {
                fileList = RenderDataUtils.getRenderDataWithPerfix(file,".ftl","");
                directory=file;
            } else {
                fileList.add(new RenderData(file.getName(),file.getName().replace(".ftl","")));
                directory = file.getParentFile();
            }
            renderFileName(fileList);
            this.render(fileList, directory);
        } else {
            System.out.println("目录或文件不存在哦");
        }
    }

    public void render(List<RenderData> fileList, File directory) {
        try{
            configuration.setDirectoryForTemplateLoading(directory);
            configuration.setEncoding(Locale.CHINA, "utf-8");
//            fileList.stream().filter(item ->
//                    item.getName().contains(".ftl")
//            ).collect(Collectors.toList());


            for (RenderData file : fileList) {
                try {
                    Template template = configuration.getTemplate(file.getTemplateName());
                    File outFile = new File(output, file.getRenderName());
                    if (!outFile.getParentFile().exists()) {
                        outFile.getParentFile().mkdirs();
                    }
                    Environment environment = template.createProcessingEnvironment(data, new FileWriter(outFile));
                    environment.process();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void renderFileName(List<RenderData> list) {
        for (RenderData renderData : list) {
            Matcher matcher = pattern.matcher(renderData.getRenderName());
            while (matcher.find()) {
                String group = matcher.group(1);
                Object tempRenderData = data.get(group);
                if (tempRenderData != null) {
                    renderData.setRenderName(renderData.getRenderName().replace("${"+group+"}", tempRenderData.toString()));
                }
            }
        }

    }


}
